package com.zzwtec.permission.controller;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.zzwtec.permission.model.AdPosition;
import com.zzwtec.permission.dao.AdPositionDao;
import com.zzwtec.interfaces.ResultObject;
import com.zzwtec.common.exception.BizException;
import com.zzwtec.common.util.BeanUtil;
import com.zzwtec.common.util.ErrorMsg;
import com.zzwtec.interfaces.Page;
import com.zzwtec.interfaces.RequestObject;
import com.zzwtec.interfaces.permission.dto.AdPositionDTO;
import com.zzwtec.interfaces.permission.services.AdPositionService;
/**
 * 微服务AdPositionService接口实现类
 * @author  草原狼
 * 
 * 2017年08月02日
 */
@RestController
public class AdPositionController implements AdPositionService {
	
	@Autowired
	private DiscoveryClient discoveryClient;

	@Resource(name = "adPositionDTO")
	private AdPositionDao adPositionDao;

	@Override
	public ResultObject<String> addAdPosition(@RequestBody RequestObject<AdPositionDTO> request) {
		ResultObject<String> result = new ResultObject<String>(request);
		AdPositionDTO dto = request.getData();
		AdPosition bean = new AdPosition();
		BeanUtil.copyProperties(bean, dto);		
		adPositionDTO.insert(bean);
		result.setData(bean.getId());
		return result;
		
	}
	
	@Override
	public ResultObject<Void> removeAdPosition(@RequestBody RequestObject<String> request) {
		ResultObject<Void> result = new ResultObject<Void>(request);
		adPositionDao.deleteById(request.getData());
		return result;	
		
	}


	@Override
	public ResultObject<Void> modifyAdPosition(@RequestBody RequestObject<AdPositionDTO> request) {
		ResultObject<Void> result = new ResultObject<Void>(request);
		AdPositionDTO dto = request.getData();
		AdPosition bean = new AdPosition();
		BeanUtil.copyProperties(bean, dto);		
		adPositionDao.update(bean);
		return result;
		
	}


	@Override
	public ResultObject<List<AdPositionDTO>> queryAllAdPosition(@RequestBody RequestObject<Void> request) {
		ResultObject<List<AdPositionDTO>> result = new ResultObject<List<AdPositionDTO>>(request);
		List<AdPosition> list = adPositionDao.selectAll();
		List<AdPositionDTO> data = new ArrayList<AdPositionDTO>();
		for(AdPosition entity : list){
			AdPositionDTO dto = new AdPositionDTO();
			BeanUtil.copyProperties(dto, entity);
			data.add(dto);
		}
		result.setData(data);
		return result;	
		
	}


	@Override
	public ResultObject<AdPositionDTO> queryAdPositionById(@RequestBody RequestObject<String> request) {
		ResultObject<AdPositionDTO> result = new ResultObject<AdPositionDTO>(request);
		AdPosition entity = adPositionDao.selectOne(request.getData());
		AdPositionDTO dto = new AdPositionDTO();
		BeanUtil.copyProperties(dto, entity);
		result.setData(dto);
		return result;	
		
	}

	@Override
	public ResultObject<Page<AdPositionDTO>> queryAdPositionByPage(@RequestBody RequestObject<Page<AdPositionDTO>> request) {
		ResultObject<Page<AdPositionDTO>>  result=new ResultObject<Page<AdPositionDTO>>(request);
		Page<AdPositionDTO> page=request.getData();
		Page<AdPosition> rPage=new Page<AdPosition>();
		rPage.setCondition(page.getCondition());
		rPage.setDistinct(page.isDistinct());
		rPage.setGroupByFields(page.getGroupByFields());
		rPage.setOrderField(page.getOrderField());
		rPage.setOrderType(page.getOrderType());
		rPage.setPage(page.getPage());
		rPage.setPageNum(page.getPageNum());
		rPage.setSearchFields(page.getSearchFields());
		rPage.setSearchValues(page.getSearchValues());
		rPage = adPositionDao.selectPage(rPage);
		if(rPage.getData()!=null){
			List<AdPositionDTO> data = new ArrayList<AdPositionDTO>(rPage.getData().size());
			for(AdPosition entity : rPage.getData()){	
				AdPositionDTO dto = new AdPositionDTO();
				BeanUtil.copyProperties(dto, entity);
				data.add(dto);
			}
			page.setData(data);
		}
		page.setTotal(rPage.getTotal());
		page.setRows(rPage.getRows());
		result.setData(page);
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;
	}
	
	
	@Override
	public ServiceInstance showServiceInfo() {		
		ServiceInstance localServiceInstance = this.discoveryClient.getLocalServiceInstance();
		return localServiceInstance;
	}
}
